<?php
define('IE', stristr($_SERVER['HTTP_USER_AGENT'], 'msie'));

class HTTPResponse {
	private $mime = 'text/html';
	private $charset;
	
	private $output;
	
	public function __construct($charset = 'utf-8') {
		$this->charset = $charset;
		$this->detectMime();
		$this->sendHeader();
	}
	
	public function getMeta() {
		return '<meta http-equiv="content-type" content="' . "{$this->mime}; charset={$this->charset}" . '" />';
	}
	
	public function monitor() {
		//iconv_set_encoding('internal_encoding', 'ISO-8859-1');
		//iconv_set_encoding('output_encoding', $this->charset);
		//ob_start('ob_iconv_handler');
		ob_start(array($this, '_ob_handler'));
		
		if ($this->mime == 'text/html')
			ob_start(array($this, '_ob_htmlhandler'));
		
		if($this->mime == "application/xhtml+xml") {
			echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="th" dir="ltr">';
		} else {
			echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="th">';
		}
	}
	
	public function unmonitor() {
		//ob_end_flush();
		ob_end_flush();
		if ($this->mime == 'text/html')
			ob_end_flush();
	}
	
	public function _ob_handler($text) {
		$text .= '</html>';
		$text = $this->compress($text);
		return $text;
	}
	
	public function _ob_htmlhandler($text) {
		$text = $this->fix_xhtml2html($text);
		$text = $this->compress_html($text);
		return $text;
	}
	
	private function fix_xhtml2html($buffer) {
		$buffer = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
					'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', $buffer);
		return (str_replace(' />', '>', $buffer));
	}
	
	private function compress_html($text) {
		$unneeded = array('</li>', '</p>');
		
		foreach ($unneeded as $u)
			$text = str_ireplace($u, '', $text);
		
		return $text;
	}
	
	private function compress($text) {
		$text = preg_replace('/[\s]+/',' ',$text);
		$text = preg_replace('/<!--[^\[].*?-->/','',$text);
		$unneeded = array(
			'> <' => '><',
			'> ' => '>',
			' <' => '<',
		);
		
		foreach ($unneeded as $k=>$u)
			$text = str_ireplace($k, $u, $text);
			
		return $text;
	}
	
	private function detectMime() {
		// Check for XHTML-supported browser.
		if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
			// if there's a Q value for "application/xhtml+xml" then also 
   			// retrieve the Q value for "text/html" to see preferred encoding.
   			
			if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i", $_SERVER["HTTP_ACCEPT"], $matches)) {
				$xhtml_q = $matches[1];
				if(preg_match("/text\/html;q=0(\.[1-9]+)/i", $_SERVER["HTTP_ACCEPT"], $matches)) {
					$html_q = $matches[1];
					// if the Q value for XHTML is greater than or equal to that 
					// for HTML then use the "application/xhtml+xml" mimetype
					if($xhtml_q >= $html_q) {
						$this->mime = "application/xhtml+xml";
					}
				}
			// if there was no Q value, then just use the 
			// "application/xhtml+xml" mimetype
			} else {
				$this->mime = "application/xhtml+xml";
			}
		}
		
		// W3C_Validator's special case
		if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
			$this->mime = "application/xhtml+xml";
		}
	}
	
	private function sendHeader() {
		//header($a = "Content-Type: {$this->mime}; charset={$this->charset}");
		//header("Vary: *");
	}
}
